package mest.controller.hws;


import com.alibaba.fastjson.JSON;
import mest.entity.bomsave_manytomany;
import mest.service.hws.BomSaveManytomanyService;
import mest.service.system.DBService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/*
 * 物料存货多对多
 * 物料选择存货
 * */
@RequestMapping("/bomsavemanytomany")
@RestController
@CrossOrigin
public class BomSaveManytomanyController {

    @Resource
    private BomSaveManytomanyService bomSaveManytomanyService;
    @Autowired
    private DBService dbService;

    /*
    物料选择存货时增加中间表数据
    中间表新增
    */
    @RequestMapping("/insertModel")
    public String insertModelh(@RequestBody List<bomsave_manytomany> bomSaveManytomany) {
        System.out.println("进入物料存货多对多操作");
        String theirDb = dbService.getCompanyDB();
        System.out.println(bomSaveManytomany.size());
        Map<String, Object> map = new HashMap<String, Object>();
        int num = bomSaveManytomanyService.insert(theirDb,bomSaveManytomany);
        if (num > 0) {
            map.put("code", 0);
            map.put("msg", "操作成功");
        } else {
            map.put("code", 1);
            map.put("msg", "操作失败");
        }
        return JSON.toJSONString(map);
    }

    //中间表修改
    //物料没有修改功能
   /* @RequestMapping("/updateByone")
    public String updateByone(BomSaveManytomany bomSaveManytomany){
        System.out.println("更新物料存货关系");
        Map<String, Object> map = new HashMap<String, Object>();
        int num = bomSaveManytomanyService.updateByPrimaryKey(bomSaveManytomany);
        if (num > 0) {
            map.put("status", "ok");
            map.put("data", "操作成功");
        } else {
            map.put("status", "error");
            map.put("data", "操作失败");
        }
        return JSON.toJSONString(map);
    }*/

    //中间表删除
    @RequestMapping("/delModel")
    public String delModelh(Integer id) {
        System.out.println("删除物料存货多对多关系");
        String theirDb = dbService.getCompanyDB();
        Map<String, Object> map = new HashMap<String, Object>();
        int num = bomSaveManytomanyService.deleteByPrimaryKey(theirDb,id);
        if (num > 0) {
            map.put("code", 0);
            map.put("msg", "操作成功");
        } else {
            map.put("code", 1);
            map.put("msg", "操作失败");
        }
        return JSON.toJSONString(map);
    }

    //根据物料档案id查询所有关联存货档案
    @RequestMapping("/selectAll")
    public String selectAllh(Integer bomid) {
        System.out.println("根据物料档案id查询所有关联存货档案");
        String theirDb = dbService.getCompanyDB();
        Map<Object, Object> map = new HashMap<Object, Object>();
        try {
            int num = bomSaveManytomanyService.selectPrimarycount(theirDb,bomid);
            List<bomsave_manytomany> bomSaveManytomanyList = bomSaveManytomanyService.selectByPrimaryKey(theirDb,bomid);
            if (num>0) {
                map.put("code", 0);
                map.put("msg", "success");
                map.put("count", num);
                map.put("data", bomSaveManytomanyList);
            }else {
                map.put("code", 1);
                map.put("msg", "暫無存貨檔案");
            }
        } catch (Exception e) {
            map.put("msg", "fales");
            map.put("errmsg", e.toString());
        }
        return JSON.toJSONString(map);
    }
}
